 
import { ABaseDecision } from "GameLoop/DecisionMaking/BaseDecision";
import { MainStrategy } from "GameLoop/WorldState/EntityManager/CreateStrategy/MainStrategy";
import { getRoleConfig } from "utils/constants/bodyconstant";
import { priority } from "utils/constants/definetypes";
import { BodyUtils } from "utils/creep/bodyutils";
import { Roles } from "utils/creep/setups";
import { PositionHelper } from "utils/position/PositionHelper";
import { CreepTaskHelper } from "utils/task/CreepTaskHelper";
import { generateUniqueId } from "utils/utils";

/**
 * 清理spawn 和 controller lab container 的决策
 */
export class TransportDecision< T extends "creep"="creep"> extends ABaseDecision<T>
{

    public isExpired(event: NormalEvent, region: string): boolean 
    {
       
        const room = Game.rooms[event.room];
        const lordroom = Game.rooms[event.region] as Required<Room>;


        if(lordroom.canExecute(1000) && lordroom.controller.level>=6)
        {
            if(lordroom.getTaskById(`${region}_clear_container`,["creep"]))
            {
                return false
            }
            return true;
        }
        return false;
    }
    public makeTask(event: IFillStoreEvent, region: string): IDecisionCode<T>|false
    {
        // global.Log.Info(`维修任务`);
        const lordroom = Game.rooms[event.region] as Required<Room>;
        const controllerEntity = lordroom.controller!;

         
        const coreEntity = lordroom.getEntity("CoreEntity");
        const store = coreEntity.getStores({resourceType:RESOURCE_ENERGY,amount:2000});
        if(store.length==0)
        {
            return false;
        }
        // 控制器
        {
            const container = controllerEntity.getContainer();
            for(const e of container)
            {
                if(e.store.getUsedCapacity() == e.store.getUsedCapacity(RESOURCE_ENERGY))
                {
                    continue;
                }
               
               
                const Task = this.getTask(region,e,store[0],[RESOURCE_ENERGY] );
                return Task;
            }
        }
        // spawn
        {
            const spawn = lordroom.getEntity("SpawnEntity");
            const contrainers = spawn.getContainer().map(e=>Game.getObjectById(e));
            for(const e of contrainers)
            {
                if(!e)
                {
                    continue
                }
                if(e.store.getUsedCapacity() == e.store.getUsedCapacity(RESOURCE_ENERGY))
                {
                    continue;
                }
                const Task = this.getTask(region,e,store[0],[RESOURCE_ENERGY] );
                return Task;
            }
        }
        
        // lab 
        {
            const lab = lordroom.getEntity("LabEntity");
            const contrainers = lab.getContainer().map(e=>Game.getObjectById(e));
            for(const e of contrainers)
            {
                if(!e)
                {
                    continue
                }
                if(e.store.getUsedCapacity() <500)
                {
                    continue;
                }
                const Task = this.getTask(region,e,store[0],[] );
                return Task;
            }
        }
        return false;
    }
    

    private getTask(region:string,from:AnyStoreStructure,to:AnyStoreStructure,exclude :ResourceConstant[]=[])
    {
        const keys = Object.keys(from.store) as ResourceConstant[];
        const list :ITransportItem[]= [];
        for(const e of keys)
        {
            if(exclude.includes(e))
            {
                continue
            }
            list.push({
                From:PositionHelper.getObjectRef(from),
                To:PositionHelper.getObjectRef(to),
                resourceType:e,
                amount:from.store.getUsedCapacity(e) as number,
            })
        }
    
        const Taskarg:ICreepTaskData<"transportAccurate"> = 
        {
            type: "transportAccurate",
            arg: {
                List: list
            },
            room:region,
            creeps: [],
            body: {},
            income: -1,
            disableSpawn: true,
            required: [
                [['carry'], 1,1]
            ],
            cost: 30
        }
        
        const code:IDecisionCode<T>={
            region:region,
            isunique:false,
            task:[{
                id:`${region}_clear_container`,
                ttl:5000,
                priority:priority.F,
                type:"creep",
                data:Taskarg,
                shard:1,
            }],
        }
        return code;
    }
}